/**  
* @Title: ShopPicController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月20日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.controller.BaseController;
import com.lemon.model.cityRun.crShopPic;
import com.lemon.model.cityRun.crShopPicType;
import com.lemon.service.cityRun.CrShopPicService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: ShopPicController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月20日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/pic")
public class ShopPicController extends BaseController {

	@Resource
	private CrShopPicService picService;

	/**
	 * <b>Description:<blockquote> 获取相册列表
	 * </blockquote></b>
	 * 
	 * @Title: getShopPicTypeList
	 * @param pt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@RequestMapping("/getShopPicTypeList")
	public ReqResult getShopPicTypeList(@ModelAttribute crShopPicType pt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status != 0  ");
			pt.setWhere(buffer.toString());
			pt.setOrderBy(" order by a.sort asc, a.create_time desc ");
			List<crShopPicType> list = picService.selectPicTypeListBySelective(pt);
			if (list.size() <= 0) {
				pt.setTitle("默认相册");
				pt.setIsDefault(1);
				pt.setSort(99);
				pt.setStatus(1);
				picService.insertPicTypeBySelective(pt);
				pt.setId(IdTypeHandler.encode(Long.parseLong(pt.getId())));
				list.add(pt);
			}

			for (int i = 0; i < list.size(); i++) {
				crShopPicType picType = list.get(i);
				if (StringUtil.isEmpty(picType.getPic())) {
					//查询出来第一个图片的信息
					pt = new crShopPicType();
					crShopPic pic = new crShopPic();
					pic.setPicTypeId(picType.getId());
					pic.setStatus(1);
					List<crShopPic> list2 = picService.selectPicListBySelective(pic);
					if (list2.size() > 0) {
						pt.setPic(list2.get(0).getPic());
						list.get(i).setPic(list2.get(0).getPic());
						pt.setId(picType.getId());
						picService.updatePicTypeBySelective(pt);
					}
				}
			}

			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询相册列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 获取相册图片列表
	 * </blockquote></b>
	 * 
	 * @Title: getShopPicTypeList
	 * @param pt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@RequestMapping("/getShopPicList")
	public ReqResult getShopPicList(@ModelAttribute crShopPic pic , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status != 0  ");
			pic.setWhere(buffer.toString());
			pic.setOrderBy(" order by a.create_time desc ");
			List<crShopPic> list = picService.selectPicListBySelective(pic);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询相册列表失败");
		}
		return reqResult;
	}

	@RequestMapping("/getShopPicAuditList")
	public ReqResult getShopPicAuditList(@ModelAttribute crShopPic pic , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append("  and a.status = 3  ");
			pic.setWhere(buffer.toString());
			List<crShopPic> list = picService.selectPicAuditListBySelective(pic);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询相册列表失败");
		}
		return reqResult;
	}

	@RequestMapping("/updateAuditPic.do")
	public ReqResult updateAuditPic(@ModelAttribute crShopPic pic , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String ids = request.getParameter("ids");
			String status = request.getParameter("status");

			picService.updataAuditPic(ids , Integer.parseInt(status));
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询相册列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 根据id获取相册信息
	 * </blockquote></b>
	 * 
	 * @Title: getShopPicTypeList
	 * @param pt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@RequestMapping("/getShopPicTypeById")
	public ReqResult getShopPicTypeById(@ModelAttribute crShopPicType pt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<crShopPicType> list = picService.selectPicTypeListBySelective(pt);

			reqResult.setReturnResult(list.size() > 0 ? list.get(0) : "");
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询相册列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加相册
	 * </blockquote></b>
	 * 
	 * @Title: addShopPicType
	 * @param pt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@RequestMapping("/addShopPicType.do")
	public ReqResult addShopPicType(@ModelAttribute crShopPicType pt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			pt.setStatus(1);
			picService.insertPicTypeBySelective(pt);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "添加相册失败");
		}
		return reqResult;
	}
	/**
	 * <b>Description:<blockquote> 添加相册
	 * </blockquote></b>
	 * 
	 * @Title: addShopPicType
	 * @param pt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月20日
	 */
	@RequestMapping("/updateShopPicType.do")
	public ReqResult updateShopPicType(@ModelAttribute crShopPicType pt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			picService.updatePicTypeBySelective(pt);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改相册失败");
		}
		return reqResult;
	}

	@RequestMapping("/updateShopPic.do")
	public ReqResult updateShopPic(@ModelAttribute crShopPic pt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("ids");
			String mids = request.getParameter("moveids");

			if (StringUtil.isNotBlank(ids)) {
				crShopPic u2 = new crShopPic();
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					u2.setStatus(0);
					u2.setId(id);
					picService.updatePicBySelective(u2);
					reqResult.setReturnResult(1);
				}
			} else {
				if (StringUtil.isNotBlank(mids)) {
					crShopPic u2 = new crShopPic();
					String[] split = mids.split(",");
					for (int i = 0; i < split.length; i++) {
						String id = split[i];
						u2.setId(id);
						u2.setPicTypeId(pt.getPicTypeId());
						picService.updatePicBySelective(u2);
						reqResult.setReturnResult(1);
					}
				} else {
					picService.updatePicBySelective(pt);
					reqResult.setReturnResult(1);
				}
			}
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改相册失败");
		}
		return reqResult;
	}
}
